package com.hmall.api.config;

import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;

@Slf4j
public class DefaultFeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                log.info("userId = {}", userId);
                if (userId == null) {
                    return ;
                }
                requestTemplate.header("user-info", userId.toString());
            }
        };
    }

    @Bean
    public Logger.Level feignLogLevel() {
        return Logger.Level.FULL;
    }

}
